

package com.yuke.cloud.service.opc.mq.consumer;

import com.google.common.base.Preconditions;
import com.yuke.cloud.common.util.JacksonUtil;
import com.yuke.cloud.service.api.opc.dto.SendEmailRequest;
import com.yuke.cloud.service.opc.service.OpcSendMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Set;

/**
 * The class Opc send email topic consumer.
 *
 * @author
 */
@Slf4j
@Service
public class OpcSendEmailConsumer {

	@Resource
	private OpcSendMailService opcSendMailService;

	/**
	 * Handler send email topic.
	 *
	 * @param body      the body
	 */
	public void handlerSendEmail(String body) {
		Preconditions.checkArgument(!StringUtils.isEmpty(body), "发送邮件失败, 消息体不能为空");
		SendEmailRequest request;
		try {
			request = JacksonUtil.parseJson(body, SendEmailRequest.class);
		} catch (Exception e) {
			log.error("发送邮件MQ出现异常={}", e.getMessage(), e);
			throw new IllegalArgumentException("处理MQ信息,JSON转换异常");
		}
		String subject = request.getSubject();
		String text = request.getText();
		Set<String> to = request.getTo();
		opcSendMailService.sendTemplateMail(subject, text, to);
	}
}
